package com.blacksheep.mtm.service.Impl;

import com.blacksheep.mtm.common.exception.XmallException;
import com.blacksheep.mtm.dao.TbSupplierMapper;
import com.blacksheep.mtm.dto.MemberDto;
import com.blacksheep.mtm.pojo.common.DataTablesResult;
import com.blacksheep.mtm.pojo.manager.TbSupplier;
import com.blacksheep.mtm.pojo.manager.TbSupplierExample;
import com.blacksheep.mtm.service.SupplierService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private TbSupplierMapper supplierMapper;

    @Override
    public DataTablesResult getSupplierList(int draw, int start, int length, int[] state, String search, String minDate, String maxDate, String orderCol, String orderDir) {
        DataTablesResult result=new DataTablesResult();

        PageHelper.startPage(start/length+1,length);
        List<TbSupplier> list = supplierMapper.selectList("%"+search+"%",state,minDate,maxDate,orderCol,orderDir);
        PageInfo<TbSupplier> pageInfo=new PageInfo<>(list);
        result.setRecordsFiltered((int)pageInfo.getTotal());
        result.setDraw(draw);
        result.setData(list);

        return result;
    }

    @Override
    public TbSupplier insert(TbSupplier supplier) {
        int result = supplierMapper.insertSelective(supplier);
        if (result<1){
            throw new XmallException("新增失败");
        }
        return supplier;
    }

    @Override
    public int deletBatch(Integer[] ids) {
        int result = supplierMapper.deleteBatchById(ids);
        if(result!=ids.length){
            throw new XmallException("删除失败");
        }
        return 0;
    }

    @Override
    public TbSupplier updateSupplier(TbSupplier supplier) {
        int result = supplierMapper.updateByPrimaryKeySelective(supplier);
        if (result<1){
            throw new XmallException("更新失败");
        }
        return null;
    }

    @Override
    public TbSupplier alertState(Integer id, Integer state) {
        int result = supplierMapper.updateStateByPrimaryKey(id,state);
        if (result<1){
            throw new XmallException("更新失败");
        }
        return null;
    }

    @Override
    public List<TbSupplier> selectListFront() {
        TbSupplierExample example = new TbSupplierExample();
        example.createCriteria().andStateNotEqualTo(-1);
        List<TbSupplier> list = supplierMapper.selectByExample(example);
        return list;
    }
}
